package com.scvtc.utils;

import com.scvtc.domain.dto.common.SystemLoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Description: Security框架工具类
 * @Author: yiFei
 * @date: 2022/2/16 23:08
 */
public class SecurityUtil {

    private SecurityUtil() {

    }

    /**
     * 获取用户
     **/
    public static SystemLoginUser getSystemLoginUser() {
        return (SystemLoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 判断是否是系统管理员
     *
     * @return
     */
    public static Boolean isAdmin() {
        Long id = getSystemLoginUser().getUserInfo().getId();
        return id != null && 1L == id;
    }

    /**
     * 获取登录用户 Id
     *
     * @return
     */
    public static Long getUserId() {
        return getSystemLoginUser().getUserInfo().getId();
    }

}
